package service;

import Dao.CartDao;
import domain.Cart;
import domain.Product;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class CartService {
    private ProductService productService=new ProductService();//便于在购物车表里面显示商品信息
    private CartDao cartDao=new CartDao();
    //添加商品到购物车的方法
    public boolean addCart(Integer productID,Integer myQuantity,Integer ClientID) throws SQLException,
            IOException, NoSuchFieldException, ClassNotFoundException,
            InstantiationException, IllegalAccessException {
        Product product=productService.findProductById(productID);
        int i= 0;
        if (product!=null) {
            i = cartDao.update("insert into cart values(null,?,?,?,?,?,?)",productID,
                    product.getProductName(),product.getRetailPrice(),myQuantity,
                    product.getModel(),ClientID);
        }else{
            return false;
        }
        if(i>0){
            return true;
        }
        return false;
    }
    //将商品从购物车中移除
    public boolean removeCart(Integer productID,Integer ClientID) throws SQLException, IOException,
            ClassNotFoundException {
        int i=cartDao.update("delete from cart where productID=? and ClientID=?",productID,ClientID);
        if(i>0){
            return true;
        }
        return false;
    }
    //显示购物车的所有元素
    public List<Cart> getCartList(Integer ClientID) throws SQLException,
            IOException, NoSuchFieldException,
            ClassNotFoundException, InstantiationException, IllegalAccessException {
        return cartDao.queryMulti("select * from Cart where ClientID=?",Cart.class,ClientID);
    }
    //修改购物车的商品数量
    public boolean updateQuantity(Integer productID,Integer myQuantity,Integer ClientID) throws SQLException,
            IOException, ClassNotFoundException, NoSuchFieldException,
            InstantiationException, IllegalAccessException {
        if(!isExist(productID,ClientID)){
            System.out.println("商品不存在于购物车！无法修改！");
            return false;
        }
        if (myQuantity>0) {
            int i=cartDao.update("update cart set myQuantity=? " +
                            "where ClientID=? and ProductID=?",myQuantity,
                    ClientID,productID);
            if(i>0){
                return true;
            }
            return false;
        }else{
            removeCart(productID,ClientID);
            return true;
        }

    }
    //根据商品的ID返回Cart对象(找到购物车物品)
    public Cart findCartRecordById(Integer id,Integer ClientID) throws SQLException, IOException,
            NoSuchFieldException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        return cartDao.querySingle("select * from cart where ProductID=? and ClientID=?",Cart.class,id,ClientID);
    }
    //根据productID判断该商品是否存在购物车里面，防止用户输入错误的productID
    public boolean isExist(Integer id,Integer ClientID) throws SQLException, IOException, NoSuchFieldException,
            ClassNotFoundException, InstantiationException, IllegalAccessException {
        return findCartRecordById(id,ClientID)==null?false:true;
    }


}
